<?php
/**
 * 门店管理
 * Created by Sublime Text 3.
 * @authors Rejeo (rejeo@qq.com)
 * @date    2018-05-01 10:30:34
 * @version $Id$
 */
namespace app\bis\controller;

use think\Controller;
use think\Request;

class Location extends Controller
{
    // 门店管理列表
    public function index()
    {
        $locatData = model('BisLocation')->getLocatByStatus();
        $this->assign('locatData', $locatData);
        return $this->fetch();
    }

    // 添加门店
    public function add(Request $request)
    {
        // 入库操作
        if ($request->isPost()) {
            // 获取表单值
            $data = $request->post();
            // dump($data);exit;
            // 校验表单数据
            // $vail = $this->validate($data,'register.add');
            // if (TRUE !== $vail) {
            //     $this->error($vail);
            // }
            $bisId = session('bisAccount', '', 'bis')['bis_id']?:'';
            // 获取经纬度
            // $LngLat= \Baidumap::getLngLat($data['address']);
            /*if (empty($LngLat)||0!=$LngLat['status']||1!=$LngLat['result']['precise']) {
                $this->error('无法获取地址数据，或者匹配地址不精准！');
            }*/

            // 商户基本信息入库
            $city_path = empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'].','.$data['se_city_id'];
            $description = empty($data['description'])?'':$data['description'];
            // 门店相关信息入库
            $data['category'] = '';
            if (!empty($data['se_category_id'])) {
                $data['category'] = implode('|',$data['se_category_id']);
            }
            $xpoint = empty($LngLat['result']['location']['lng'])?'':$LngLat['result']['location']['lng'];
            $ypoint = empty($LngLat['result']['location']['lat'])?'':$LngLat['result']['location']['lat'];
            $locatData = [
                'bis_id'        => $bisId,
                'name'          => $data['name'],
                'tel'           => $data['tel'],
                'contact'       => $data['contact'],
                'logo'          => $data['logo'],
                'address'       => $data['address'],
                'xpoint'        => $xpoint,
                'ypoint'        => $ypoint,
                'open_time'     => $data['open_time'],
                'content'       => empty($data['content'])?'':$data['content'],
                'is_main'       => 0, // 默认为门店
                'api_address'   => $data['address'],
                'city_id'       => $data['city_id'],
                'city_path'     => $city_path,
                'category_id'   => $data['category_id'],
                'category_path' => $data['category_id'].','.$data['category'],
            ];
            $bislocatId = model('BisLocation')->add($locatData);
            if ($bislocatId) {
                $this->success('门店添加成功！');
            }
            $this->error('门店添加失败！');
        }
        // 获取一级城市
        $citys = model('city')->getNormalFirstCity();
        // 获取一级栏目
        $categorys = model('category')->getNormalFirstCategory();
        $this->assign('citys', $citys);
        $this->assign('categorys', $categorys);
        return $this->fetch();
    }

}

